//import QtQuick 1.0
import QtQuick 1.1
//import com.nokia.symbian 1.0
import com.nokia.meego 1.0

RPage {
    id:  viewer
    anchors.fill: parent
    property string fileName
    property int linesPerPage: Math.floor(height/lineHeight)
    property int fontSize: lineHeight - 2
    property real lineHeight: 26
    property real lineHeightMin: 20
    property real lineHeightMax: 70
    property int currentIndex: 0
    property real alignedContentY


    function viewerListCurrentIndex() {
        return viewerList.indexAt(viewerList.contentX+1,
                                  viewerList.contentY+1)
    }

    function updateViewerCurrentIndex() {
        viewer.currentIndex = viewerListCurrentIndex()
//        var currentContentY = viewerList.contentY
//        console.log("Before set current index, contentY", viewerList.contentY)
//        viewerList.currentIndex = viewer.currentIndex
//        console.log("After set current index, contentY", viewerList.contentY)
//        var item = viewerList.currentItem
//        console.log("currentItem.x", item.x,
//                    "y", item.y,
//                    "height", item.height)
//        viewerList.contentY = currentContentY
    }

    function updateAlignedContentY() {
        viewer.alignedContentY = viewerList.contentY
        console.log("alignedContentY:", viewer.alignedContentY)
    }

    Component {
        id: lineDelegate
        Rectangle {
            id: wrapper
            width: viewerList.width
            height: lineText.lineHeight*lineText.lineCount
            color: "transparent"
            Text {
                id: lineText
                clip: true
                anchors.top: parent.top
                width: parent.width
                text: textLine
                color: "black"
//                color: "white"
                wrapMode: Text.WordWrap
                font.pixelSize: viewer.fontSize
                lineHeight: viewer.lineHeight
                lineHeightMode: Text.FixedHeight
            }
//            Component.onCompleted: {
//                console.log("Text completed, x", x,
//                            "y", y,
//                            "height", height,
//                            "map to listViewer x", viewerList.mapFromItem(wrapper, x, y).x,
//                            "y", viewerList.mapFromItem(wrapper, x, y).y)
//            }
        }
    }

    ListView {
        id: viewerList
        property real moveFlickStartContentY
        clip: true
        cacheBuffer: 0
        anchors.centerIn: parent
        width: parent.width
        height: viewer.linesPerPage * viewer.lineHeight
        delegate: lineDelegate
        PinchArea {
            anchors.fill: parent

            onPinchFinished: {
                console.log("pinch updated: ",
                            "pinch.scale", pinch.scale,
                            "contentY", viewerList.contentY,
                            "linesPerPage", viewer.linesPerPage,
                            "lineHeight", viewer.lineHeight,
                            "listHeight", viewerList.height,
                            "viewerHeight", viewer.height);
                var currentIndex = viewerList.indexAt(viewerList.contentX,viewerList.contentY)
                console.log("currentIndex at (0,0)", currentIndex)
                //Set new lineHeight, the minimun value is viewer.lineHeightMin, max is viewer.lineHeightMax
                viewer.lineHeight = Math.min(viewer.lineHeightMax, Math.max(viewer.lineHeightMin, Math.floor(viewer.lineHeight*pinch.scale)))
                console.log("after pinch updated: ",
                            "pinch.scale", pinch.scale,
                            "contentY", viewerList.contentY,
                            "linesPerPage", viewer.linesPerPage,
                            "lineHeight", viewer.lineHeight,
                            "listHeight", viewerList.height,
                            "viewerHeight", viewer.height);
                engine.resetModel(viewerList.model)
                viewerList.positionViewAtIndex(currentIndex, ListView.Beginning)
                updateAlignedContentY()
            }
        }

        MouseArea {
            id: mouseA
            anchors.fill: parent
            onClicked: {
                console.log("clicked:", mouseX, mouseY,
                            viewer.height, viewer.lineHeight,
                            viewerList.contentY, viewerList.height, viewerList.currentIndex)
                var contentY = viewerList.contentY
                if (mouseY <= viewer.height/2) {
                    //For some reason, when item 0's y is not 0, we don't move any more
                    var currentIndex = viewerListCurrentIndex()
                    console.log("currentIndex", currentIndex)
                    if (currentIndex !== 0 /*&& currentIndex !== -1*/) {
                        contentY -= Math.min(viewerList.contentY, viewerList.height)
                    }
                    else {
                        console.log("reach the index(0), do nothing")
                    }
                }
                else {
                    contentY += viewerList.height
                }

                viewerList.contentY = contentY
                updateViewerCurrentIndex()
                console.log("new contentY", contentY, viewer.lineHeight* viewer.linesPerPage)
            }
        }
        onHeightChanged: {
            console.log("Height changed, new one:", height)
            positionViewAtIndex(viewer.currentIndex, ListView.Beginning)
            updateAlignedContentY()
        }

        onMovementStarted: {
            moveFlickStartContentY = contentY
        }
        onFlickStarted: {
            moveFlickStartContentY = contentY
        }

        function handleMoveFlickEnded() {
            //The first one, do nothing
            if (viewerListCurrentIndex() === 0) {
                console.log("already the first one, do nothing")
                return
            }


            var delta = viewerList.contentY-viewer.alignedContentY
            var abs = Math.abs(delta) - Math.floor(Math.abs(delta)/viewer.lineHeight)*viewer.lineHeight

            console.log("viewerList.contentY", viewerList.contentY,
                        "viewer.alignedContentY", viewer.alignedContentY,
                        "delta", delta,
                        "abs", abs)


            //Moving up, move up more
            if (delta < 0) {
                console.log("moving up more")
                //move minimun
                if (abs > viewer.lineHeight/2) {
                    viewerList.contentY -= viewer.lineHeight - abs
                }
                else {
                    viewerList.contentY += abs
                }
            }
            else {
                //Moving down, move down more
                console.log("moving down more")
                //move minimun
                if (abs > viewer.lineHeight/2) {
                    viewerList.contentY += viewer.lineHeight - abs
                }
                else {
                    viewerList.contentY -= abs
                }
            }

            updateViewerCurrentIndex()
            updateAlignedContentY()
        }

        onFlickEnded: {
            console.log("onFlickEnded contentY", contentY,
                        "viewer.lineHeight", viewer.lineHeight)
            handleMoveFlickEnded()
        }
        onMovementEnded: {
            console.log("onMovementEnded contentY", contentY,
                        "viewer.lineHeight", viewer.lineHeight)
            handleMoveFlickEnded()
        }
    }

    Component.onCompleted: {
        console.log("page loaded: width, height",width, height)
        var model = engine.fileModel(viewer.fileName);
        if (!model) {
            console.log("open file failed!")
        }
        else {
            viewerList.model = model
            viewerList.positionViewAtIndex(viewer.currentIndex, ListView.Beginning)

        }
    }
}
